package com.ceadeal.admin.common.datatable;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * DataTable请求参数封装
 * Created by Yzw on 2016/7/29 0029.
 */
public class DataTableInput {
    /**
     * 绘制计数器。这个是用来确保Ajax从服务器返回的是对应的（Ajax是异步的，因此返回的顺序是不确定的）。 要求在服务器接收到此参数后再返回
     */
    private Integer draw;

    /**
     * 第一条数据的起始位置
     */
    private Integer start;

    /**
     * 页显示的条数
     */
    private Integer length;

    /**
     * 全局的搜索条件
     */
    private Map<Search, String> search = new HashMap<Search, String>();

    /**
     * 排序方式
     */
    private List<Map<Order, String>> order = new ArrayList<Map<Order, String>>();

    /**
     * 列的属性
     */
    private List<Map<Column, String>> columns = new ArrayList<Map<Column, String>>();

    /**
     * 排序列别名
     */
    private String orderColumnName;
    /**
     * 排序方式
     */
    private String orderDir;
    /**
     * 页码
     */
    private Integer pageIndex;

    public DataTableInput() {
    }

    /* 枚举类型 */
    public enum Search {
        value,
        regex
    }

    public enum Order {
        column,
        dir
    }

    public enum Column {
        data,
        name,
        searchable,
        orderable,
        searchValue,
        searchRegex
    }
    /* 枚举类型 end */

    /**
     * 获取要排序列的别名
     * @return
     */
    public String getOrderColumnName() {
        String columnIndex = this.order.get(0).get(Order.column);
        return this.columns.get(Integer.parseInt(columnIndex)).get(Column.name);
    }

    /**
     * 获取排序方式
     * @return
     */
    public String getOrderDir() {
        return this.order.get(0).get(Order.dir);
    }

    /**
     * 获取页码
     * @return
     */
    public Integer getPageIndex() {
        return this.start / this.length + 1;
    }

    public Integer getDraw() {
        return draw;
    }

    public void setDraw(Integer draw) {
        this.draw = draw;
    }

    public Integer getStart() {
        return start;
    }

    public void setStart(Integer start) {
        this.start = start;
    }

    public Integer getLength() {
        return length;
    }

    public void setLength(Integer length) {
        this.length = length;
    }

    public Map<Search, String> getSearch() {
        return search;
    }

    public void setSearch(Map<Search, String> search) {
        this.search = search;
    }

    public List<Map<Order, String>> getOrder() {
        return order;
    }

    public void setOrder(List<Map<Order, String>> order) {
        this.order = order;
    }

    public List<Map<Column, String>> getColumns() {
        return columns;
    }

    public void setColumns(List<Map<Column, String>> columns) {
        this.columns = columns;
    }
}
